<?php
namespace app\index\controller;
// header('content-type:application/json');

use think\Controller;
use think\Db;

class Action extends controller
{
    /* 商品评论 */

    public function doComments()
    {
        $content = htmlspecialchars(input('content'));
        $gid = input('gid');
        $uid = input('uid');
        if ($content == null) {
            return 0;
        }
        $data = [
            'cId' => null,
            'cContent' => $content,
            'goodId' => $gid,
            'uId' => $uid,
            'cstatus' => 0
        ];
        $rs = Db::name('comments')->insert($data);
        if ($rs) {
            return 1;
        } else {
            return 0;
        }
    }

    /* 帖子评论 */ 

    public function doPostsComments()
    {
        $content = htmlspecialchars(input('content'));
        $pid = input('pid');
        $uid = input('uid');
        if ($content == null) {
            return 0;
        }
        $data = [
            'pcId' => null,
            'pcContent' => $content,
            'postsId' => $pid,
            'uId' => $uid,
            'pcstatus' => 0
        ];
        $rs = Db::name('pcomments')->insert($data);
        if ($rs) {
            return 1;
        } else {
            return 0;
        }
    }

    /* 商品收藏 */

    public function doSave()
    {
        $gid = input('gid');
        $uid = input('uid');
        $check = Db::name('saved s')->where('userId',$uid)
        ->join('goods g', 's.gId=g.gId')
        ->where('g.gId',$gid)
        ->find();
        if($check){
            $rs = Db::name('saved s')->where('userId',$uid)
            ->join('goods g', 's.gId=g.gId')
            ->where('g.gId',$gid)->update([
                'status'=>0
            ]);
            return $rs;
        }else{
            $data = [
                'sId' => null,
                'gId' => $gid,
                'userId' => $uid
            ];
            $rs = Db::name('saved')->insert($data);
            if ($rs) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    /* 帖子收藏 */

    public function doPostsSave()
    {
        $pid = input('pid');
        $uid = input('uid');
        $check = Db::name('saved s')->where('userId',$uid)
        ->join('posts p', 's.pId=p.pId')
        ->where('p.pId',$pid)
        ->find();
        if($check){
            $rs = Db::name('saved s')->where('userId',$uid)
            ->join('posts p', 's.pId=p.pId')
            ->where('p.pId',$pid)->update([
                'status'=>0
            ]);
            return $rs;
        }else{
            $data = [
                'sId' => null,
                'pId' => $pid,
                'userId' => $uid
            ];
            $rs = Db::name('saved')->insert($data);
            if ($rs) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    /* 消息发送 */

    public function doMessage()
    {
        $content = htmlspecialchars(input('content'));
        $fromid = input('fromid');
        $toid = input('toid');
        if ($content == null) {
            return 0;
        }
        $data = [
            'mId' => null,
            'mContent' => $content,
            'fromId' => $fromid,
            'toId' => $toid,
            'mTime' => date('Y-m-d H:m:s')
        ];
        $rs = Db::name('messages')->insert($data);
        if ($rs) {
            return 1;
        } else {
            return 0;
        }
    }

    /* 发布商品  */

    public function doSend()
    {
        $gPic = null;
        // TODO: 文件路径和URL路径鼓捣一下
        $file = request()->file('pic');
        if ($file) {
                $info = $file->rule('uniqid')->validate(['ext' => 'jpg,png,gif'])->move('../img');
                if ($info) {
                    // echo $info->getExtension(); //扩展名
                    // echo $info->getSaveName(); //路径
                    // echo $info->getFilename(); //文件名
                    $gPic = $info->getSaveName();
                } else {
                    return $file->getError();
                }
        }
        $uid = input('uid');
        $gTitle = htmlspecialchars(input('gTitle'));
        $check = Db::name('user')->where('userId', $uid)
            ->find();
        if (!$check) {
            // 找不到用户
            return 2;
        }
        if ($check['status'] == 1) {
            // 该用户已被禁用 不能发送商品信息
            return 4;
        }
        if ($gTitle == null) {
            // 标题不能为空
            return 3;
        }
        $baseURL = 'http://localhost/store/public/static/img/';
        //TODO: 太多判断，懒得写了
        $gdetail = htmlspecialchars(input('detail'));
        $typeid = input('typeid');
        $tags = input('tags');
        $price = input('price');
        $vDate = input('vdate');
        $data = [
            'gId' => null,
            'gTitle' => $gTitle,
            'gPic' => $gPic,
            'gDetail' => $gdetail,
            'typeId' => $typeid,
            'tags' => $tags,
            'price' => $price,
            'vDate' => $vDate,
            'uid' => $uid,
            'gstatus' => 0
        ];
        $rs = Db::name('goods')->insert($data);
        if ($rs) {
            return 1;
        } else {
            return 0;
        }
    }

    /* 发布帖子 */
    
    public function doPostsSend()
    {
        $pPic = null;
        // TODO: 文件路径和URL路径鼓捣一下
        $file = request()->file('pic');
        if ($file) {
                $info = $file->rule('uniqid')->validate(['ext' => 'jpg,png,gif'])->move('../img');
                if ($info) {
                    // echo $info->getExtension(); //扩展名
                    // echo $info->getSaveName(); //路径
                    // echo $info->getFilename(); //文件名
                    $pPic = $info->getSaveName();
                } else {
                    return $file->getError();
                }
        }
        $uid = input('uid');
        $pTitle = htmlspecialchars(input('pTitle'));
        $check = Db::name('user')->where('userId', $uid)
            ->find();
        if (!$check) {
            // 找不到用户
            return 2;
        }
        if ($check['status'] == 1) {
            // 该用户已被禁用 不能发送商品信息
            return 4;
        }
        if ($pTitle == null) {
            // 标题不能为空
            return 3;
        }
        $baseURL = 'http://localhost/store/public/static/img/';
        //TODO: 太多判断，懒得写了
        $pcontent = htmlspecialchars(input('pdetail'));
        $ptypeid = input('ptypeid');
        $ptime = input('ptime');
        $data = [
            'pId' => null,
            'pTitle' => $pTitle,
            'pPic' => $pPic,
            'pContent' => $pcontent,
            'ptypeId' => $ptypeid,
            'ptime' => $ptime,
            'uid' => $uid,
            'pstatus' => 1
        ];
        $rs = Db::name('posts')->insert($data);
        if ($rs) {
            return 1;
        } else {
            return 0;
        }
    }

    /* 更新 */

    public function upload(){

    }

    /* 删除商品评论 */

    public function delComment()
    {
        $cid = input('cid');
        $data = [
            'cstatus' => 1
        ];
        $rs = Db::name('comments')->where('cId', $cid)
            ->update($data);
        return $rs;
    }

    /* 取消想要 */

    public function cancelSave()
    { 
        $sid = input('sid');
        $data = [
            'status' => 1
        ];
        $rs = Db::name('saved')->where('sId', $sid)
            ->update($data);
        return $rs;  
        // $gid = input('gid');
        // $uid = input('uid');
        // $rs = Db::name('saved s')->where('userId',$uid)
        // ->join('goods g', 's.gId=g.gId')
        // ->where('g.gId',$gid)->update([
        //     'status'=>1
        // ]);
        // return $rs;
        // $gid = input('gid');
        // $uid = input('uid');
        // $sid = input('sid');
        // $data=[
        //     'ststus'=>1
        // ];
        // // $rs = Db::name('saved')->where('sId', $sid)
        // //     ->update($data);
        // // $rs = Db::name('saved s')->where('userId',$uid)
        // // ->join('goods g', 's.gId=g.gId')
        // // ->where('g.gId',$gid)->update($data);
        // $rs = Db::name('saved s')->where('sId',$sid)
        // ->where('userId',$uid)
        // ->join('goods g', 's.gId=g.gId')
        // ->where('g.gId',$gid)->update($data);
        // return $rs;
    }

    /* 取消帖子收藏 */

    public function cancelPostsSave()
    {
        $pid = input('pid');
        $uid = input('uid');
        $sid = input('sid');
        // $data=[
        //     'ststus'=>1
        // ];
        // $rs = Db::name('saved s')->where('userId',$uid)
        // ->join('goods g', 's.gId=g.gId')
        // ->where('g.gId',$gid)->update($data);
        $rs = Db::name('saved s')->where('sId',$sid)
        ->where('userId',$uid)
        ->join('posts p', 's.pId=p.pId')
        ->where('p.pId',$pid)->update([
            'status'=>1
        ]);
        return $rs;
    }

    /* 发送文件 */

    public function test()
    {
        $file = request()->file('pic');
        $info = $file->move('../img');
        if ($file) {
            $info = $file->validate(['size' => 15678, 'ext' => 'jpg,png,gif'])->move('../img');
            if ($info) {
                echo $info->getExtension(); //扩展名
                echo $info->getSaveName(); //路径
                echo $info->getFilename(); //文件名
            } else {
                echo $file->getError();
            }
        }
        dump($file);
        $user = input('user');
        dump($user);
    }
}
